// 使用统一配置 - 使用orderHttp实例
import { orderHttp as http } from './config';

export interface PageInfo<T> {
  list: T[];
  total: number;
  pageNum: number;
  pageSize: number;
}

// 订单状态枚举
export enum OrderStatus {
  PENDING = 'pending',      // 待接单
  CONFIRMED = 'confirmed',  // 已接单
  PREPARING = 'preparing',  // 准备中
  DELIVERING = 'delivering', // 配送中
  COMPLETED = 'completed',  // 已完成
  CANCELLED = 'cancelled'   // 已取消
}

// 订单项接口
export interface OrderItem {
  id: number | null;
  createTime: string | null;
  updateTime: string | null;
  relaOrderId: number | null;
  relaGoodsId: number | null;
  goodsName: string | null;
  goodsPrice: number | null;
  goodsImage: string | null;
  quantity: number | null;
  subtotal: number | null;
}

// 订单接口
export interface Order {
  id: number | null;
  createTime: string | null;
  updateTime: string | null;
  relaUserId: number | null;
  relaShopId: number | null;
  shopName: string | null;
  shopAddress: string | null;
  userAddress: string | null;
  status: string | null;
  totalAmount: number | null;
  discount: number | null;
  actualPayment: number | null;
  riderName: string | null;
  riderPhone: string | null;
  riderRating: number | null;
  riderAvatar: string | null;
  orderItems?: OrderItem[];
}

// 订单统计接口
export interface OrderStats {
  totalRevenue: number;
  totalOrders: number;
  newCustomers: number;
  avgRating: number;
  monthlyRevenue: number[];
  monthlyOrders: number[];
}

// 标准接口一: 根据ID查询
export const selectById = (id: number | null) => {
  return http.get<Order>(`/order/${id}`);
};

// 标准接口二: 分页查询全部
export const selectAll = (pageNum = 1, pageSize = 10) => {
  return http.get<PageInfo<Order>>('/order', {
    params: { pageNum, pageSize }
  });
};

// 标准接口三: 新增
export const insert = (pojo: Order) => {
  return http.post<Order>('/order', pojo);
};

// 标准接口四: 删除
export const deleteById = (id: number) => {
  return http.delete<Order>(`/order/${id}`);
};

// 标准接口五: 更新
export const update = (id: number | null, pojo: Order) => {
  return http.put<Order>(`/order/${id}`, pojo);
};

// 标准接口六: 条件查询
export const selectEq = (conditions: Record<string, any>) => {
  return http.post<Order[]>('/order/select-eq', conditions);
};

// 扩展接口: 根据用户ID查询订单
export const selectByUserId = (userId: number, pageNum = 1, pageSize = 10) => {
  return http.get<PageInfo<Order>>(`/order/user/${userId}`, {
    params: { pageNum, pageSize }
  });
};

// 扩展接口: 根据商店ID查询订单
export const selectByShopId = (shopId: number, pageNum = 1, pageSize = 10) => {
  return http.get<PageInfo<Order>>(`/order/shop/${shopId}`, {
    params: { pageNum, pageSize }
  });
};

// 扩展接口: 更新订单状态
export const updateStatus = (orderId: number, status: OrderStatus) => {
  return http.put<Order>(`/order/${orderId}/status`, { status });
};

// 扩展接口: 获取订单统计数据
export const getOrderStats = (shopId?: number, dateRange?: string) => {
  return http.get<OrderStats>('/order/stats', {
    params: { shopId, dateRange }
  });
};

// 扩展接口: 获取订单详情（包含订单项）
export const getOrderDetail = (orderId: number) => {
  return http.get<Order>(`/order/${orderId}/detail`);
};

// 扩展接口: 创建订单（包含订单项）
export const createOrder = (orderData: {
  order: Partial<Order>;
  orderItems: Partial<OrderItem>[];
}) => {
  return http.post<Order>('/order/create', orderData);
}; 